<?php defined('SYSPATH') or die('No direct access allowed.');


class Helper_FactoryDTO {

	/**
	 *
	 * save sportDTO and return the id
	 * @param sport_ws $sportWS
	 * @return int id assigned
	 */
	public static function sportDTOToEntity($sportWS){
		if ($sportWS==null){
			return null;
		}
		$sportDao = Helper_Service::factory("sport");
		$sport = $sportDao->getByName($sportWS->name);
		if (($sport!=null)&&($sport->id >0)){
			return $sport->id;
		}
		$data = array('sport_name' => $sportWS->name,
		    'description' => $sportWS->description);
		$sportDao->save(0,$data);
		return $sportDao->sport->id;
	}

	/**
	 *
	 * save surgeryDTO and return the id
	 * @param surgery_ws $surgeryWS
	 * @return int id assigned
	 */
	public static function surgeryDTOToEntity($surgeryWS){
		if ($surgeryWS==null){
			return null;
		}
		$surgeryDao = Helper_Service::factory("surgery");
		$surgery = $surgeryDao->getByName($surgeryWS->name);
		if (($surgery!=null)&&($surgery->id >0)){
			return $surgery->id;
		}
		$data = array('surgery_name' => $surgeryWS->name,
		    'description' => $surgeryWS->description);
		$surgeryDao->save(0,$data);
		return $surgeryDao->surgery->id;
	}

	/**
	 *
	 * save pathologyDTO and return the id
	 * @param pathology_ws $pathologyWS
	 * @return int id assigned
	 */
	public static function pathologyDTOToEntity($pathologyWS){
		if ($pathologyWS==null){
			return null;
		}
		$pathologyDao = Helper_Service::factory("pathology");
		$pathology = $pathologyDao->getByName($pathologyWS->name);
		if (($pathology!=null)&&($pathology->id >0)){
			return $pathology->id;
		}
		$data = array('pathology_name' => $pathologyWS->name,
			'initials' => $pathologyWS->initials,
		    'description' => $pathologyWS->description);
		$pathologyDao->save(0,$data);
		return $pathologyDao->pathology->id;
	}

	/**
	 *
	 * save medicineDTO and return the id
	 * @param medicine_ws $medicineWS
	 * @return int id assigned
	 */
	public static function medicineDTOToEntity($medicineWS){
		if ($medicineWS==null){
			return null;
		}
		$medicineDao = Helper_Service::factory("medicine");
		$medicine = $medicineDao->getByName($medicineWS->name);
		if (($medicine!=null)&&($medicine->id >0)){
			return $medicine->id;
		}
		$data = array('medicine_name' => $medicineWS->name,
		    'description' => $medicineWS->description);
		$medicineDao->save(0,$data);
		return $medicineDao->medicine->id;
	}

	/**
	 *
	 * save familyDTO and return the id
	 * @param family_ws $familyWS
	 * @return int id assigned
	 */
	public static function familyDTOToEntity($familyWS){
		if ($familyWS==null){
			return null;
		}

		$familyDao = Helper_Service::factory("family");
		$family = $familyDao->getByName($familyWS->name);
		if (($family!=null)&&($family->id >0)){
			return $family->id;
		}
		$data = array('family_name' => $familyWS->name,
		    'sex' => $familyWS->sex ?"M":"F");
		$familyDao->save(0,$data);
		return $familyDao->family->id;
	}

	/**
	 *
	 * save medicDTO and return the id
	 * @param medic_ws $medicWS
	 * @return int id assigned
	 */
	public static function medicDTOToEntity($medicWS){
		if ($medicWS==null){
			return null;
		}
		$medicDao = Helper_Service::factory("medic");
		$data = array();
		if ((property_exists($medicWS, "identity_card"))&&($medicWS->identity_card!=null)){
			$data['doctype']= $medicWS->identity_card->type_id;
			$data['identity_card_number']= $medicWS->identity_card->number;
		}else{
			$data['doctype']=null;
			$data['identity_card_number']=null;
		}
		$data['surname'] = $medicWS->surname;
		$data['name'] = $medicWS->name;
		$data['birthdate']= $medicWS->birthdate;
		$data['address']= $medicWS->address;
		$data['phone'] = $medicWS->telephone;
		$data['cel']= $medicWS->cellphone;
		$data['email'] = $medicWS->email;
		$data['sex'] = is_null($medicWS->sex) ? $medicWS->sex : ($medicWS->sex)=="M";
		$data['city'] = $medicWS->city;
		$data['specialty']= "";
		$medicDao->save(0,$data);
		return $medicDao->medic->id;
	}

	/**
	 *
	 * save patientTO and return the id
	 * @param patient_ws $patientWS
	 * @return int id assigned
	 */
	public static function patientDTOToEntity($patientWS){
		if ($patientWS==null){
			return null;
		}
		$patientDao =  Helper_Service::factory("patient");
		$data = array();
		if ((property_exists($patientWS, "identity_card"))&&($patientWS->identity_card!=null)){
			$data['doctype']= $patientWS->identity_card->type_id;
			$data['identity_card_number']= $patientWS->identity_card->number;
		}else{
			$data['doctype']= null;
			$data['identity_card_number']= null;
		}
		$data['surname'] = $patientWS->surname;
		$data['name'] = $patientWS->name;
		$data['birthdate']= $patientWS->birthdate;
		$data['address']= $patientWS->address;
		$data['phone'] = $patientWS->telephone;
		$data['cel']= $patientWS->cellphone;
		$data['email'] = $patientWS->email;
		$data['sex'] = $patientWS->sex ? "Masculino" : "Femenino";
		$data['city'] = $patientWS->city;
		$data['profession'] = $patientWS->profession_id;
		$data['medic_id'] = $patientWS->medic_id;
		// guardo los datos principales
		$patientDao->save(0,$data);

		$patientID = $patientDao->patient->id;
		

		// se guardan los habitos
		Helper_FactoryDTO::patientHabitsDTOToEntity($patientWS->habits, $patientID);

		// lleno antecedentes familiares
		if ((property_exists($patientWS, "family_background"))&&($patientWS->family_background!=null)){
			$familyPatientDao = Helper_Service::factory("familypatient",$patientID);
			$familyBackgroundArray = (is_array($patientWS->family_background)) ? $patientWS->family_background : array($patientWS->family_background);
			foreach ($familyBackgroundArray as $familyBackgroundWS) {
				$dataFB = array();
				$dataFB['family_id']=$familyBackgroundWS->family_id;
				$dataFB['patient_id']=$patientID;
				$dataFB['background_cardiac']=$familyBackgroundWS->background_cardiac ? 1 : 0;
				$dataFB['background_cholesterol']=$familyBackgroundWS->background_diabetic ? 1 : 0;
				$dataFB['background_diabetic']=$familyBackgroundWS->background_cholesterol ? 1 : 0;
				$dataFB['background_hypertension']=$familyBackgroundWS->background_hypertension ? 1 : 0;
				$familyPatientDao->save(0,$dataFB);
						

			}
		}
			
		// lleno deportes
		if  ((property_exists($patientWS, "sports"))&&($patientWS->sports!=null)){
			$sportPatientDao = Helper_Service::factory("sportpatient",$patientID);
			$sportPatientArray = is_array($patientWS->sports)? $patientWS->sports : array($patientWS->sports);
			foreach ($sportPatientArray as $sportWS) {
				$dataSp = array();
				$dataSp['patient_id']=$patientID;
				$dataSp['sport_id']=$sportWS->sport_id;
				$dataSp['age_at_start'] = $sportWS->age_at_start ? $sportWS->age_at_start:0 ;
				$dataSp['age_at_stop'] =0 ;
				$dataSp['hours_by_week'] = $sportWS->hours_x_week ? $sportWS->hours_x_week : 0;
				$sportPatientDao->save(0,$dataSp);
			}
		}


		// lleno cirugias
		if  ((property_exists($patientWS, "surgeries"))&&($patientWS->surgeries!=null)){
			$surgeryPatientDao = Helper_Service::factory("surgerypatient",$patientID);
			$surgeryPatientArray = is_array($patientWS->surgeries) ? $patientWS->surgeries : array($patientWS->surgeries);
			foreach ($surgeryPatientArray as $surgeryWS) {
				$dataSr = array();
				$dataSr['surgery_id']= $surgeryWS->surgery_id;
				$dataSr['patient_id']= $patientID;
				$dataSr['age_at_surgery']= $surgeryWS->age_at_surgery ? $surgeryWS->age_at_surgery : 0;
				$dataSr['observations']= "";
				$surgeryPatientDao->save(0,$dataSr);
			}
		}


		//lleno medicamentos
		if ((property_exists($patientWS, "medicines"))&&($patientWS->medicines!=null)){
			$medicinePatientDao = Helper_Service::factory("medicinepatient",$patientID);
			$medicinePatientArray = is_array($patientWS->medicines) ? $patientWS->medicines : array($patientWS->medicines);
			foreach ($medicinePatientArray as $medicineWS) {
				$dataM['medicine_id']=$medicineWS->medicine_id;
				$dataM['patient_id']=$patientID;
				$dataM['age_at_start']=$medicineWS->age_at_start ? $medicineWS->age_at_start : 0;
				$dataM['age_at_stop'] = 0;
				$dataM['dosage']=$medicineWS->dosage;
				$medicinePatientDao->save(0,$dataM);
			}
		}

		//lleno patologias
		if ((property_exists($patientWS, "pathologies"))&&($patientWS->pathologies!=null)){
			$pathologyPatientDao = Helper_Service::factory("pathologypatient",$patientID);
			$pathologyPatientArray = is_array($patientWS->pathologies) ? $patientWS->pathologies : array($patientWS->pathologies);
			foreach ($pathologyPatientArray as $pathology) {
				$dataM['pathology_id']=$pathology->pathology_id;
				$dataM['patient_id']=$patientID;
				$dataM['age_at_start']=$pathology->age_at_start ? $pathology->age_at_start : 0;
				$dataM['age_at_stop'] = 0;
				$dataM['currently']=$pathology->currently ? 1 : 0;
				$dataM['inherited'] = $pathology->inherited ? 1 : 0;
				$pathologyPatientDao->save(0,$dataM);
			}
		}
		

		return $patientDao->patient->id;
	}

	/**
	 *
	 * save analysisTO and return the id
	 * @param analysis_ws $analysisWS
	 * @return int id assigned
	 */
	public static function analysisDTOToEntity($analysisWS){
		if ($analysisWS==null){
			return null;
		}
		$analysisDao = Helper_Service::factory("analysis");
		$data = array();
		$data["patient_id"] = $analysisWS->patient_id;
		$data["medic_id"] = $analysisWS->medic_id;
		$data["creation_date"] = $analysisWS->date;
		$data["analysis_date"]= $analysisWS->date;
		$data["file_name"] = $analysisWS->raw_signal->file_name;
		$data["raw_signal_id"]= Helper_FactoryDTO::rawSignalDTOToEntity($analysisWS->raw_signal, $analysisWS->signal_frequency_id);
		$data["analysis_result_id"]= Helper_FactoryDTO::analysisResultDTOToEntity($analysisWS->result);
		$data["report_id"] = Helper_FactoryDTO::reportDTOToEntity($analysisWS->report) ;
		$data["final_segment_id"] = Helper_FactoryDTO::finalSignalDTOToEntity($analysisWS->final_signal);
		$data["period_id"] = $analysisWS->signal_frequency_id;
		$data["clinical_info_id"] = $analysisWS->clinical_info_id;
		$analysisDao->save(0,$data);
		
		$analysisID = $analysisDao->analysis->id;
		
		// lleno segmentos seleccionados
		if ((property_exists($analysisWS, "selected_segments"))&&($analysisWS->selected_segments!=null)){
			$segmentsArray = (is_array($analysisWS->selected_segments)) ? $analysisWS->selected_segments : array($analysisWS->selected_segments);
			foreach ($segmentsArray as $segment) {
				$segmentsDao = Helper_Service::factory("selectedsegment",$analysisID);
				$dataSeg = array();
				$dataSeg['analysis_id']=$analysisID;
				$dataSeg['number']=$segment->number;
				$dataSeg['segment']=Helper_FactoryDTO::segmentDTOToEntity($segment->segment)->serialize();
				$segmentsDao->save(0,$dataSeg);
		
		
			}
		}
		
		return $analysisDao->analysis->id;
	}


	/**
	 *
	 * save rawSignalDTO and return the id
	 * @param raw_signal_ws $rawSignalWS
	 * @return int id assigned
	 */
	public static function rawSignalDTOToEntity($rawSignalWS, $sample_period){
		if ($rawSignalWS==null){
			return null;
		}
		$rawSignalDao = Helper_Service::factory("rawsignal");
		$data = array();
		$data["sampling_period"] =  $sample_period;
		$data["filename"] = $rawSignalWS->file_name;
		$data["signal"] = Helper_FactoryDTO::segmentDTOToEntity($rawSignalWS->signal)->serialize();
		
		$rawSignalDao->save(0,$data);
		return $rawSignalDao->rawsignal->id;
	}


	public static function segmentDTOToEntity($segmentWS){
		if ($segmentWS==null){
			return null;
		}
		$segment = Model::factory("segment");
		$segment->setSamples($segmentWS->point);
		
		return $segment;
	}


	public static function finalSignalDTOToEntity($finalSignalWS){
		if ($finalSignalWS==null){
			return null;
		}
		$finalSignalDao = Helper_Service::factory("finalsegment");
		
		$data = array();
		$data["final_segment"] = Helper_FactoryDTO::segmentDTOToEntity($finalSignalWS->final_segment)->serialize();
		$data["first_derivative"] = Helper_FactoryDTO::segmentDTOToEntity($finalSignalWS->first_derivatite)->serialize();
		$data["second_derivative"] = Helper_FactoryDTO::segmentDTOToEntity($finalSignalWS->second_derivative)->serialize();
		$data["third_derivative"] = Helper_FactoryDTO::segmentDTOToEntity($finalSignalWS->third_derivative)->serialize();
		$data["fourth_derivative"] = Helper_FactoryDTO::segmentDTOToEntity($finalSignalWS->fourth_derivative)->serialize();
		
		$finalSignalDao->save(0,$data);
		return $finalSignalDao->final_segment->id;
		
		
	}

	/**
	 *
	 * save analysisResulDTO and return the id
	 * @param analysis_result_ws $analysisResulWS
	 * @return int id assigned
	 */
	public static function analysisResultDTOToEntity($analysisResultWS){
		if ($analysisResultWS==null){
			return null;
		}
		$analysisResultDao = Helper_Service::factory("analysisresult");
		$data = array();
		$data["aod_x1"]= $analysisResultWS->aod->x1 ;
		$data["aod_y1"]= $analysisResultWS->aod->y1 ;
		$data["aod_x2"]= $analysisResultWS->aod->x2 ;
		$data["aod_y2"]= $analysisResultWS->aod->y2 ;
		$data["aos_x1"]=$analysisResultWS->aos->x1;
		$data["aos_y1"]=$analysisResultWS->aos->y1;
		$data["aos_x2"]=$analysisResultWS->aos->x2;
		$data["aos_y2"]=$analysisResultWS->aos->y2;
		$data["iar_x1"]= $analysisResultWS->iar->x1 ;
		$data["iar_y1"]= $analysisResultWS->iar->y1 ;
		$data["iar_x2"]= $analysisResultWS->iar->x2 ;
		$data["iar_y2"]= $analysisResultWS->iar->y2 ;
		$data["t_x1"]= $analysisResultWS->systolic_delay->x1;
		$data["t_y1"]= $analysisResultWS->systolic_delay->y1;
		$data["t_x2"]= $analysisResultWS->systolic_delay->x2;
		$data["t_y2"]= $analysisResultWS->systolic_delay->y2;
		$data["heart_rate"]= $analysisResultWS->heart_rate;
		$analysisResultDao->save(0,$data);
		return $analysisResultDao->analysis_result->id;
	}

	/**
	 *
	 * save reportDTO and return the id
	 * @param report_ws $reportWS
	 * @return int id assigned
	 */
	public static function reportDTOToEntity($reportWS){
		if ($reportWS==null){
			return null;
		}
		$reportDao = Helper_Service::factory("report");
		$data = array();
		$data['comment_aod'] = $reportWS->comment_aod;
		$data['comment_aos'] = $reportWS->comment_aos;
		$data['comment_iar'] = $reportWS->comment_iar;
		$data['comments'] = $reportWS->comments;
		$data['conclusion'] = $reportWS->conclusion;
		$data['habits_Summary'] = $reportWS->habits_summary;
		$data['background_family'] = $reportWS->background_family;
		$data['background_medicines'] = $reportWS->background_medicines;
		$data['background_pathologies'] = $reportWS->background_pathologies;
		$data['background_surgeries'] = $reportWS->background_surgeries;
		$reportDao->save(0,$data);
		return $reportDao->report->id;
	}

	/**
	 *
	 * save signalFrequencyDTO and return the id
	 * @param signal_frequency_ws $signalFrequencyWS
	 * @return int id assigned
	 */
	public static function signalFrequencyDTOToEntity($signalFrequencyWS){
		if ($signalFrequencyWS==null){
			return null;
		}
		$signalFrequencyDao = Helper_Service::factory("samplingperiod");
		$frequency = $signalFrequencyDao->getByName($signalFrequencyWS->name);
		if (($frequency!=null)&&($frequency->id >0)){
			return $frequency->id;
		}
		$data = array('pc_description' => $signalFrequencyWS->name,
		    'k_value' => $signalFrequencyWS->frequency);
		$signalFrequencyDao->save(0,$data);
		return $signalFrequencyDao->samplingperiod->id;
	}

	/**
	 *
	 * save clinicalInfoDTO and return the id
	 * @param clinical_info_ws $clinicalInfoWS
	 * @return int id assigned
	 */
	public static function clinicalInfoDTOToEntity($clinicalInfoWS){
		if ($clinicalInfoWS==null){
			return null;
		}
		$clinicalInfoDao =  Helper_Service::factory("clinicalinformation");
		$data = array();
		$data['information_date'] = $clinicalInfoWS->date;
		$data['age'] = $clinicalInfoWS->age;
		$data['height'] = $clinicalInfoWS->height;
		$data['weight'] = $clinicalInfoWS->weight;
		$data['total_cholesterol'] = $clinicalInfoWS->total_cholesterol;
		$data['hdl'] = $clinicalInfoWS->hdl;
		$data['ldl'] = $clinicalInfoWS->ldl;
		$data['triglycerides'] =$clinicalInfoWS->triglycerides;
		$data['glucemic']=$clinicalInfoWS->glucemic;
		$data['systolic_pressure']=$clinicalInfoWS->systolic_pressure;
		$data['diastolic_pressure']=$clinicalInfoWS->diastolic_pressure;
		$data['patient_id']=$clinicalInfoWS->patient_id;
		$data['observations'] = '';
		$clinicalInfoDao->save(0,$data);
		return $clinicalInfoDao->clinicalInfo->id;
	}


	/**
	 *
	 * save identityCardDTO and return the id
	 * @param identity_card_type_ws $identityCardWS
	 * @return int id assigned
	 */
	public static function identityCardTypeDTOToEntity($identityCardWS){
		if ($identityCardWS==null){
			return null;
		}
		$identityCardDao = Helper_Service::factory("identitycard");
		$identityCard = $identityCardDao->getByName($identityCardWS->name);
		if (($identityCard!=null)&&($identityCard->id >0)){
			return $identityCard->id;
		}
		$data = array('description'=>$identityCardWS->name);
		$identityCardDao->save(0, $data);
		return $identityCardDao->identitycard->id;
	}

	/**
	 *
	 * save professionDTO and return the id
	 * @param profession_ws $professionWS
	 * @return int id assigned
	 */
	public static function professionDTOToEntity($professionWS){
		if ($professionWS==null){
			return null;
		}
		$professionDao = Helper_Service::factory("profession");
		$profession = $professionDao->getByName($professionWS->name);
		if (($profession!=null)&&($profession->id >0)){
			return $profession->id;
		}
		$data = array('profession' => $professionWS->name,
		    'description' => $professionWS->description);
		$professionDao->save(0,$data);
		return $professionDao->profession->id;
	}

	/**
	 *
	 * save habitsDTO and return the id
	 * @param patient_habits_ws $patientHabitsWS
	 * @return int id assigned
	 */
	public static function patientHabitsDTOToEntity($patientHabitsWS, $patientID){
		if ($patientHabitsWS==null){
			return null;
		}
		$habitsDao = Helper_Service::factory("habit");
		$data = array();
		$data['smoke']=$patientHabitsWS->smoking ? 1 : 0;
		$data['smoked']=$patientHabitsWS->smoked ? 1 : 0;
		$data['years_old_at_smooke_start']=$patientHabitsWS->age_at_start ? $patientHabitsWS->age_at_start : 0;
		$data['years_old_at_smooke_finish']=$patientHabitsWS->age_at_end ? $patientHabitsWS->age_at_end  : 0;
		$data['cigarettes_per_day']=$patientHabitsWS->cigarettes_per_day ? $patientHabitsWS->cigarettes_per_day : 0;
		$data['automedication']=$patientHabitsWS->automedication ? 1 : 0;
		$data['angry_level']=$patientHabitsWS->angry_level ? $patientHabitsWS->angry_level : 0;
		$data['stress_level']=$patientHabitsWS->stress_level ? $patientHabitsWS->stress_level : 0;
		$data['blocks_walked_per_day']=$patientHabitsWS->blocks_walked_per_day ? $patientHabitsWS->blocks_walked_per_day : 0;
		$data['consume_alcohol']=$patientHabitsWS->consumming_alcohol ? 1 : 0;
		$data['sedentary']=$patientHabitsWS->sedentary ? 1 : 0;
		$data['work_place']=$patientHabitsWS->work_place;
		$data['patient_id'] = $patientID;
		$habitsDao->save(0,$data);
		return $habitsDao->habit_patient->id;
	}

	/**
	 *
	 * Generate sportDTO from sport
	 * @param sport $sport
	 * @return sport_ws
	 */
	public static function entityToSportDTO($sport){
		if ($sport==null){
			return null;
		}
		$sportWS = new sport_ws();
		$sportWS->id = $sport->id;
		$sportWS->name = $sport->sport_name;
		$sportWS->description = $sport->description;
		return $sportWS;
	}

	/**
	 *
	 * Generate surgeryDTO from surgery
	 * @param surgery $surgery
	 * @return surgery_ws
	 */
	public static function entityToSurgeryDTO($surgery){
		if ($surgery==null){
			return null;
		}
		$surgeryWS = new surgery_ws();
		$surgeryWS->id = $surgery->id;
		$surgeryWS->name = $surgery->surgery_name;
		$surgeryWS->description = $surgery->description;
		return $surgeryWS;
	}

	/**
	 *
	 * Generate pathologyDTO from pathology
	 * @param pathology $pathology
	 * @return pathology_ws
	 */
	public static function entityToPathologyDTO($pathology){
		if ($pathology==null){
			return null;
		}
		$pathologyWS = new pathology_ws();
		$pathologyWS->id = $pathology->id;
		$pathologyWS->name = $pathology->pathology_name;
		$pathologyWS->description = $pathology->description;
		$pathologyWS->initials = $pathology->initials;
		return $pathologyWS;
	}

	/**
	 *
	 * Generate medicineDTO from medicine
	 * @param medicine $medicine
	 * @return medicine_ws
	 */
	public static function entityToMedicineDTO($medicine){
		if ($medicine==null){
			return null;
		}
		$medicineWS = new medicine_ws();
		$medicineWS->id = $medicine->id;
		$medicineWS->name = $medicine->name;
		$medicineWS->description = $medicine->description;
		return $medicineWS;
	}

	/**
	 *
	 * Generate familyDTO from family
	 * @param family $family
	 * @return family_ws
	 */
	public static function entityToFamilyDTO($family){
		if ($family==null){
			return null;
		}
		$familyWS = new family_ws();
		$familyWS->id = $family->id;
		$familyWS->name = $family->family_name;
		$familyWS->sex = (($family->sex)=="M");
		return $familyWS;
	}

	/**
	 *
	 * Generate medicDTO from medic
	 * @param medic $medic
	 * @return medic_ws
	 */
	public static function entityToMedicDTO($medic){
		if ($medic==null){
			return null;
		}
		$medicWS = new medic_ws();
		$medicWS->id = $medic->id;
		$medicWS->name=$medic->name;
		$medicWS->surname = $medic->surname;
		$medicWS->birthdate = $medic->birthdate;
		$medicWS->sex = is_null($medic->sex) ? $medic->sex : $medic->sex =="M" ;
		$medicWS->address = $medic->address;
		$medicWS->telephone = $medic->phone;
		$medicWS->cellphone = $medic->cel;
		$medicWS->email = $medic->email;
		$medicWS->city = $medic->city;
		if (($medic->doctype_id!=null)&&($medic->identity_card_number!=null)){
			$identityCard = new identity_card_ws();
			$identityCard->number= $medic->identity_card_number;
			$identityCard->type_id = $medic->doctype_id;
			$medicWS->identity_card = $identityCard;
		}else {
			$medicWS->identity_card = null;
		}
		return $medicWS;
	}

	/**
	 *
	 * Generate patientDTO from patient
	 * @param patient $patient
	 * @return patient_ws
	 */
	public static function entityToPatientDTO($patient){
		if ($patient==null){
			return null;
		}
		$patientWS = new patient_ws();
		$patientWS->id = $patient->id;
		$patientWS->name = $patient->name;
		$patientWS->surname= $patient->surname;
		$patientWS->birthdate = $patient->birthdate;
		$patientWS->sex = (($patient->sex)=="M");
		$patientWS->telephone =$patient->phone;
		$patientWS->cellphone = $patient->cel;
		$patientWS->city = $patient->city;
		$patientWS->email = $patient->email;
		$patientWS->address = $patient->address;
		$patientWS->medic_id = $patient->medic_id;
		$patientWS->profession_id = $patient->profession_id;


		if (($patient->doctype_id!=null)&&($patient->identity_card_number!=null)){
			$identityCard = new identity_card_ws();
			$identityCard->number= $patient->identity_card_number;
			$identityCard->type_id = $patient->doctype_id;
			$patientWS->identity_card = $identityCard;
		}else {
			$patientWS->identity_card = null;
		}

		$patientWS->habits = self::entityToPatientHabitsDTO( Helper_Service::factory("habit")->getPatientHabits($patient->id));
		
		// load all patient family backgrounds
		$familyPatientDao = Helper_Service::factory("familypatient");
		$familyBackgroundsDTO = array();
		$familyBackgrounds = $familyPatientDao->getPatientFamily($patient->id);
		$i=0;
		foreach ($familyBackgrounds as $familyBackground){		
			$family = new patient_family_ws();
			$family->family_id = $familyBackground->family_id;
			$family->background_cardiac =  $familyBackground->background_cardiac;
			$family->background_cholesterol =  $familyBackground->background_cholesterol;
			$family->background_diabetic =  $familyBackground->background_diabetic;
			$family->background_hypertension =  $familyBackground->background_hypertension;
			$familyBackgroundsDTO[$i]=$family;
			$i++;
		}

		$patientWS->family_background=$familyBackgroundsDTO;

		// load all patient sports
		$sportPatientDao = Helper_Service::factory("sportpatient");
		$sportsDTO = array();
		$sports = $sportPatientDao->getPatientSports($patient->id);
		$i=0;
		foreach ($sports as $sportpatient){
			$sport = new patient_sport_ws();
			$sport->sport_id = $sportpatient->sport_id;
			$sport->age_at_start =  $sportpatient->age_at_start;
			$sport->hours_x_week =  $sportpatient->hours_by_week;
			$sportsDTO[$i]=$sport;
			$i++;
		}
		$patientWS->sports = $sportsDTO;

		// load all patient surgeries
		$surgeryPatientDao = Helper_Service::factory("surgerypatient");
		$surgeriesDTO = array();
		$surgeries = $surgeryPatientDao->getPatientSurgeries($patient->id);
		$i=0;
		foreach ($surgeries as $surgeryPatient){
			$surgery = new patient_surgery_ws();
			$surgery->surgery_id = $surgeryPatient->surgery_id;
			$surgery->age_at_surgery =  $surgeryPatient->age_at_surgery;
			$surgeriesDTO[$i]=$surgery;
			$i++;
		}
		$patientWS->surgeries = $surgeriesDTO;

		// load all patient medicines
		$medicinePatientDao = Helper_Service::factory("medicinepatient");
		$medicinesDTO = array();
		$medicines = $medicinePatientDao->getPatientMedicines($patient->id);
		$i=0;
		foreach ($medicines as $medicinePatient){
			$medicine = new patient_medicine_ws();
			$medicine->medicine_id = $medicinePatient->medicine_id;
			$medicine->age_at_start =  $medicinePatient->age_at_start;
			$medicine->dosage =  $medicinePatient->dosage;
			$medicinesDTO[$i]=$medicine;
			$i++;
		}
		$patientWS->medicines = $medicinesDTO;

		// load all patient pathologies
		$pathologyPatientDao = Helper_Service::factory("pathologypatient");
		$pathologiesDTO = array();
		$pathologies = $pathologyPatientDao->getPatientPathologies($patient->id);
		$i=0;
		foreach ($pathologies as $pathologyPatient){
			$pathology = new patient_pathology_ws();
			$pathology->pathology_id = $pathologyPatient->pathology_id;
			$pathology->age_at_start =  $pathologyPatient->age_at_start;
			$pathology->currently =  $pathologyPatient->currently;
			$pathology->inherited = $pathologyPatient->inherited;
			$pathologiesDTO[$i]=$pathology;
			$i++;
		}
		$patientWS->pathologies = $pathologiesDTO;
		
		
		return $patientWS;
	}

	/**
	 *
	 * Generate analysisDTO from analysis
	 * @param analysis $analysis
	 * @return analysis_ws
	 */
	public static function entityToAnalysisDTO($analysis){
		if ($analysis==null){
			return null;
		}
		//TODO: falta segmentos seleccionados y finalSignal
		$analysisWS = new analysis_ws();
		$analysisWS->id = $analysis->id;
		$analysisWS->date = $analysis->analysis_date;
		$analysisWS->medic_id = $analysis->medic_id;
		$analysisWS->patient_id = $analysis->patient_id;
		$analysisWS->raw_signal = self::entityToRawSignalDTO($analysis->getRawSignal());
		$analysisWS->report = self::entityToReportDTO($analysis->getReport());
		$analysisWS->result = self::entityToAnalysisResultDTO($analysis->getResults());
		$analysisWS->signal_frequency_id = $analysis->signal_frequency_id;
		$analysisWS->clinical_info_id = $analysis->clinical_info_id;
		$analysisWS->final_signal =  $analysis->final_signal;
		$analysisWS->selected_segments =null;
		return $analysisWS;
	}

	/**
	 *
	 * Generate rawSignalDTO from rawsignal
	 * @param rawsignal $rawSignal
	 * @return raw_signal_ws
	 */
	public static function entityToRawSignalDTO($rawSignal){
		if ($rawSignal==null){
			return null;
		}
		$rawSignalWS = new raw_signal_ws();
		$rawSignalWS->file_name = $rawSignal->getFilename();
		$rawSignalWS->signal = self::entityToSegmentDTO($rawSignal->getSamples());
		return $rawSignalWS;
	}

	/**
	 *
	 * Generate segmentDTO from segment
	 * @param segment $segment
	 * @return segment_ws
	 */
	public static function entityToSegmentDTO($segment){
		if ($segment==null){
			return null;
		}
		$segmentWS = new segment_ws();
		$segmentWS->point =$segment->samples;
		return $segmentWS;
	}

	/**
	 *
	 * Generate finalSignalDTO from finalsegment
	 * @param finalsegment $finalSignal
	 * @return final_signal_ws
	 */
	public static function entityToFinalSignalDTO($finalSignal){
		if ($finalSignal==null){
			return null;
		}
		$finalSignalWS = new final_signal_ws();
		$finalSignalWS->final_segment = self::entityToSegmentDTO($finalSignal->getFinalSegment());
		$finalSignalWS->first_derivatite = self::entityToSegmentDTO($finalSignal->getFirstDerivative());
		$finalSignalWS->second_derivative = self::entityToSegmentDTO($finalSignal->getSecondDerivative());
		$finalSignalWS->third_derivative = self::entityToSegmentDTO($finalSignal->getThirdDerivative());
		$finalSignalWS->fourth_derivative = self::entityToSegmentDTO($finalSignal->getFourthDerivative());
		$finalSignalWS->exponential =null;
		return $finalSignalWS;
	}

	/**
	 *
	 * Generate analysisResultDTO from analysisresult
	 * @param analysisresult $analysisResult
	 * @return analysis_result_ws
	 */
	public static function entityToAnalysisResultDTO($analysisResult){
		if ($analysisResult==null){
			return null;
		}
		$analysisResultWS = new analysis_result_ws();
		$analysisResultWS->heart_rate = $analysisResult->heart_rate;

		$xyAOD = new parameter_coord_ws();
		$xyAOD->x1 = $analysisResult->aod_x1;
		$xyAOD->x2 = $analysisResult->aod_x2;
		$xyAOD->y1 = $analysisResult->aod_y1;
		$xyAOD->y2 = $analysisResult->aod_y2;

		$xyAOS = new parameter_coord_ws();
		$xyAOS->x1 = $analysisResult->aos_x1;
		$xyAOS->x2 = $analysisResult->aos_x2;
		$xyAOS->y1 = $analysisResult->aos_y1;
		$xyAOS->y2 = $analysisResult->aos_y2;

		$xyIAR = new parameter_coord_ws();
		$xyIAR->x1 = $analysisResult->iar_x1;
		$xyIAR->x2 = $analysisResult->iar_x2;
		$xyIAR->y1 = $analysisResult->iar_y1;
		$xyIAR->y2 = $analysisResult->iar_y2;

		$xyT= new parameter_coord_ws();
		$xyT->x1 = $analysisResult->t_x1;
		$xyT->x2 = $analysisResult->t_x2;
		$xyT->y1 = $analysisResult->t_y1;
		$xyT->y2 = $analysisResult->t_y2;

		$analysisResultWS->aos = $xyAOS;
		$analysisResultWS->aod  = $xyAOD;
		$analysisResultWS->iar  = $xyIAR;
		$analysisResultWS->systolic_delay  = $xyT;
		return $analysisResultWS;
	}

	/**
	 *
	 * Generate reportDTO from report
	 * @param report $report
	 * @return report_ws
	 */
	public static function entityToReportDTO($report){
		if ($report==null){
			return null;
		}
		$reportWS = new report_ws();
		$reportWS->comments = $report->comments;
		$reportWS->conclusion = $report->conclusion;
		$reportWS->comment_iar= $report->comment_iar;
		$reportWS->comment_aos= $report->comment_aos;
		$reportWS->comment_aod= $report->comment_aod;
		$reportWS->background_surgeries= $report->background_surgeries;
		$reportWS->background_family= $report->background_family;
		$reportWS->background_pathologies= $report->background_pathologies;
		$reportWS->background_medicines= $report->background_medicines;
		$reportWS->habits_summary= $report->habits_summary;
		return $reportWS;
	}


	public static function entityToAverageDTO($average){
		if ($average==null){
			return null;
		}
		$averageWS = new averages_ws();
		$averageWS->decade = $average->decade;
		$averageWS->sex = $average->sex;
		$averageWS->samples = $average->amount;
		$averageWS->aod_avg= $average->aod_avg;
		$averageWS->aod_trust_interval= $average->aod_trust;
		$averageWS->aos_avg= $average->aos_avg;
		$averageWS->aos_trust_interval= $average->aos_trust;
		$averageWS->iar_avg= $average->air_avg;
		$averageWS->iar_trust_interval= $average->iar_trust;
		return $averageWS;
	}

	/**
	 *
	 * Generate signalFrequencyDTO from samplingperiod
	 * @param samplingperiod $signalFrequency
	 * @return signal_frequency_ws
	 */
	public static function entityToSignalFrequencyDTO($signalFrequency){
		if ($signalFrequency==null){
			return null;
		}
		$signalFrequencyWS = new signal_frequency_ws();
		$signalFrequencyWS->id = $signalFrequency->id;
		$signalFrequencyWS->name = $signalFrequency->pc_description;
		$signalFrequencyWS->frequency = $signalFrequency->k_value;
		return $signalFrequencyWS;
	}

	/**
	 *
	 * Generate cliniclaInfoDTO from clinicalinformation
	 * @param clinicalinformation $clinicalInfo
	 * @return clinical_info_ws
	 */
	public static function entityToClinicalInfoDTO($clinicalInfo){
		if ($clinicalInfo==null){
			return null;
		}
		$clinicalInfoWS = new clinical_info_ws();
		$clinicalInfoWS->id = $clinicalInfo->id;
		$clinicalInfoWS->date = $clinicalInfo->information_date;
		$clinicalInfoWS->height = $clinicalInfo->height;
		$clinicalInfoWS->weight = $clinicalInfo->weight;
		$clinicalInfoWS->age =  $clinicalInfo->age;
		$clinicalInfoWS->hdl =  $clinicalInfo->hdl;
		$clinicalInfoWS->ldl =  $clinicalInfo->ldl;
		$clinicalInfoWS->total_cholesterol =  $clinicalInfo->total_cholesterol;
		$clinicalInfoWS->systolic_pressure =  $clinicalInfo->systolic_pressure;
		$clinicalInfoWS->diastolic_pressure =  $clinicalInfo->diastolic_pressure;
		$clinicalInfoWS->triglycerides =  $clinicalInfo->triglycerides;
		$clinicalInfoWS->glucemic = $clinicalInfo->glucemic;
		$clinicalInfoWS->patient_id = $clinicalInfo->patient_id;
		return $clinicalInfoWS;
	}


	/**
	 *
	 * Generate identityCardDTO from identitycard
	 * @param identitycard $identityCard
	 * @return identity_card_type_ws
	 */
	public static function entityToIdentityCardTypeDTO($identityCard){
		if ($identityCard==null){
			return null;
		}
		$identityCardWS = new identity_card_type_ws();
		$identityCardWS->id = $identityCard->id;
		$identityCardWS->name = $identityCard->description;
		return $identityCardWS;
	}


	/**
	 *
	 * Generate professionDTO from profession
	 * @param profession $profession
	 * @return profession_ws
	 */
	public static function entityToProfessionDTO($profession){
		if ($profession==null){
			return null;
		}
		$professionWS = new profession_ws();
		$professionWS->id = $profession->id;
		$professionWS->name = $profession->profession;
		$professionWS->description = $profession->description;
		return $professionWS;
	}

	/**
	 *
	 * Generate patientHabitsDTO from habit
	 * @param habit $patientHabits
	 * @return patient_habits_ws
	 */
	public static function entityToPatientHabitsDTO($patientHabits){
		if ($patientHabits==null){
			return null;
		}
		$patientHbitsWS = new patient_habits_ws();
		$patientHbitsWS->smoked = $patientHabits->smoked;
		$patientHbitsWS->smoking = $patientHabits->smoke;
		$patientHbitsWS->cigarettes_per_day = $patientHabits->cigarettes_per_day;
		$patientHbitsWS->age_at_start = $patientHabits->years_old_at_smooke_start;
		$patientHbitsWS->age_at_end = $patientHabits->years_old_at_smooke_finish;
		$patientHbitsWS->automedication = $patientHabits->automedication;
		$patientHbitsWS->consumming_alcohol = $patientHabits->consume_alcohol;
		$patientHbitsWS->sedentary = $patientHabits->sedentary;
		$patientHbitsWS->blocks_walked_per_day = $patientHabits->blocks_walked_per_day;
		$patientHbitsWS->stress_level = $patientHabits->stress_level;
		$patientHbitsWS->work_place = $patientHabits->work_place;
		$patientHbitsWS->angry_level = $patientHabits->angry_level;
		return $patientHbitsWS;
	}

}?>
